import { defineStore } from "pinia"
import type { UserState } from "./type"
import { reqUserLogin } from "@/api/hos";
import type { UserInfo, LoginData, UserLoginResponseData } from '@/api/hos/type'
import { storage_token, get_token, remove_token } from "@/utils/token";

export default defineStore("userStore", {
    state: (): UserState => ({
        userInfo: JSON.parse(get_token() as string) || {} as UserInfo,
        dialogVisible: false
    }),
    actions: {
        //用户手机号码登录方法
        async userLogin(loginData: LoginData) {
            //登录请求
            let result: UserLoginResponseData = await reqUserLogin(loginData);
            if (result.code == 200) {
                this.userInfo = result.data;
                //本地存储持久化存储用户信息
                storage_token(JSON.stringify(this.userInfo));
                //返回一个成功的Promise
                return 'ok';
            } else {
                return Promise.reject(new Error(result.message));
            }
        },
    },
    getters: {

    }
})